package com.wuyou.securitydemo.userdetails;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;
import java.util.List;

public class CustomizedUser extends User {

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    /**
     * 用户ID
     */
    @Getter
    private final Long id;

    /**
     * 部门ID
     */
    @Getter
    private final Long deptId;

    /**
     * 手机信息
     */
    @Getter
    private final String mobile;

    /**
     * 租户ID
     */
    @Getter
    @Setter
    @JsonSerialize(using = ToStringSerializer.class)
    private Long tenantId;

    /**
     * 用户昵称
     */
    @Getter
    private String displayName;

    /**
     * 用户昵称
     */
    @Getter
    @Setter
    private String companyCode;

    /**
     * 租户名称
     */
    @Getter
    @Setter
    private String tenantName;

    /**
     * 租户详情
     */
    @Getter
    @Setter
    private TenantVO tenantVO;

    /**
     * 顶级部门配置的租户列表 id list
     */
    @Getter
    private List<Long> tenantList;

    /**
     * Construct the <code>User</code> with the details required by {@link DaoAuthenticationProvider}.
     *
     * @param id                    用户ID
     * @param username              the username presented to the <code>DaoAuthenticationProvider</code>
     * @param password              the password that should be presented to the <code>DaoAuthenticationProvider
     *                              </code>
     * @param enabled               set to <code>true</code> if the user is enabled
     * @param accountNonExpired     set to <code>true</code> if the account has not expired
     * @param credentialsNonExpired set to <code>true</code> if the credentials have not expired
     * @param accountNonLocked      set to <code>true</code> if the account is not locked
     * @param authorities           the authorities that should be granted to the caller if they presented the
     *                              correct username and password and the user is enabled. Not null.
     * @throws IllegalArgumentException if a <code>null</code> value was passed either as a parameter
     *                                  or as an element in the <code>GrantedAuthority</code> collection
     */
    public CustomizedUser(@JsonProperty("id") Long id,
                          @JsonProperty("deptId") Long deptId,
                          @JsonProperty("username") String username,
                          @JsonProperty("password") String password,
                          @JsonProperty("mobile") String mobile,
                          @JsonProperty("enabled") boolean enabled,
                          @JsonProperty("accountNonExpired") boolean accountNonExpired,
                          @JsonProperty("credentialsNonExpired") boolean credentialsNonExpired,
                          @JsonProperty("accountNonLocked") boolean accountNonLocked,
                          @JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities,
                          @JsonProperty("displayName") String displayName,
                          @JsonProperty("tenantList") List<Long> tenantList) {
        super(
                username,
                password,
                enabled,
                accountNonExpired,
                credentialsNonExpired,
                accountNonLocked,
                authorities);
        this.id = id;
        this.deptId = deptId;
        this.mobile = mobile;
        this.displayName = displayName;
        this.tenantList = tenantList;
    }
}
